package com.qiniuyun.graph.common.service.impl;

import com.qiniuyun.graph.common.enums.GraphEnum;
import com.qiniuyun.graph.common.service.CompoundService;
import com.qiniuyun.graph.common.utils.Base64Util;
import com.qiniuyun.graph.common.vo.CompoundVO;
import com.qiniuyun.graph.common.vo.GraphVO;
import org.springframework.stereotype.Service;

@Service
public class CompoundServiceImpl implements CompoundService {
    @Override
    public String compoundGraph(CompoundVO compoundVO) {
        StringBuilder sb = new StringBuilder();
        sb.append(compoundVO.getBackground() + "?watermark/3");
        for (GraphVO graphVO : compoundVO.getDress()) {
            sb.append(this.joinParams(graphVO));
        }
        return sb.toString();
    }

    @Override
    public String joinParams(GraphVO graphVO) {
        StringBuilder sb = new StringBuilder();
        //文字
        if (graphVO.getType().equals(GraphEnum.text.type())) {
            //设置类型
            sb.append("/text/");
            //填充内容
            sb.append(Base64Util.encodeToString(graphVO.getContent()) + "/font/5b6u6L2v6ZuF6buR/fill/");
            //填充颜色
            sb.append(Base64Util.encodeToString(graphVO.getColor()));
            //填充字体大小
            sb.append("/fontsize/" + graphVO.getFontSize());
            //填充坐标
            sb.append("/dissolve/100/dx/" + graphVO.getXPosition() + "/dy/" + graphVO.getYPosition());
            //设置坐标原点
            sb.append("/gravity/" + graphVO.getGravity());
        } else if (graphVO.getType().equals(GraphEnum.image.type())) {
            //设置类型
            sb.append("/image/");
            //设置图片地址
            sb.append(Base64Util.encodeToString(graphVO.getUrl()));
            //设置坐标
            sb.append("/dissolve/100/dx/" + graphVO.getXPosition() + "/dy/" + graphVO.getYPosition());
            //设置坐标原点
            sb.append("/gravity/" + graphVO.getGravity());
            //设置缩放比例
            sb.append("/ws/" + graphVO.getWs());
        }
        return sb.toString();
    }
}
